/**
 * 用户列表处理JS
 */

$(function(){
	
	//新增用户
	$("#addUserBtn").click(function(){
		$.layer({
		    type: 2,
		    shade: [0],
		    fix: false,
		    title: '新增用户',
		    maxmin: false,
		    iframe: {src : global.rootPath + "/user/add?type=init"},
		    area: ['350px' , '290px'],
		    close: function(index){
		    }
		});
		
	});
	
	//查询
	$("#btnSearch").click(function(){
		var formSerialize = $("#form").serializeObject();
		var v = JSON.stringify(formSerialize);
		$("#v").val(encodeURIComponent(v));
		form.submit();
	});
});

function userAuth(id) {
	$.layer({
	    type: 2,
	    shade: [0],
	    fix: false,
	    title: '用户授权',
	    maxmin: false,
	    iframe: {src : global.rootPath + "/user/auth?type=init&id="+id},
	    area: ['350px' , '400px'],
	    close: function(index){
	    }
	});
}

function saveAuth() {
    var nodes = $("input[name='id']:checked");
    if(nodes.length <= 0) {
    	parent.layer.alert("必须选择一个功能点", 8);
    } else {
    	var nodesId = "";
    	$("input[name='id']:checked").each(function(){
    		nodesId = nodesId + $(this).val() + ",";
    	});
    	var id = $("#id").val();
    	
    	security.ajax({
    		url : global.rootPath + "/user/auth?type=saveAuth",
    		data : {
    			id : id,
    			roleIds : nodesId
    		},
    		beforeSend : function(){
    			security.showLoading();
    		},
    		success : function(code,msg) {
    			parent.layer.msg(msg, 2);
				parent.location.reload();
    			var index = parent.layer.getFrameIndex(window.name);
    			parent.layer.close(index);
    		},
    		error : function(message) {
    			security.showError(message);
    		}
    	});
    	
    }
    
    
} 

function cancel(id){
	security.ajax({
		url : global.rootPath + "/user/cancel",
		data : {
			id : id
		},
		beforeSend : function(){
			security.showLoading();
		},
		success : function(code,msg) {
			security.showSuccess(msg,refresh);
		},
		error : function(message) {
			security.showError(message);
		}
	});
}

function setPwd(id){
	$.layer({
	    type: 2,
	    shade: [0],
	    fix: false,
	    title: '分配临时密码',
	    maxmin: false,
	    iframe: {src : global.rootPath + "/user/setPwd?userId="+id},
	    area: ['400px' , '300px'],
	    close: function(index){
	    }
	});
}

function refresh() {
	var formSerialize = $("#form").serializeObject();
	var v = JSON.stringify(formSerialize);
	$("#v").val(encodeURIComponent(v));
	form.submit();
}